package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbItemExample;
import com.dongdongshop.service.TbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class TbItemServiceImpl implements TbItemService {

    @Autowired
    private TbItemMapper tbItemMapper;

    @Override
    public void insertGoodInfo(TbItem tbItem) {
        tbItemMapper.insertSelective(tbItem);
    }

    @Override
    public List<TbItem> selectItemByGoodsId(Long goodsId) {
        TbItemExample te = new TbItemExample();
        TbItemExample.Criteria criteria = te.createCriteria();
        // 状态为正常的
        criteria.andStatusEqualTo("1");
        // 库存大于 0
        criteria.andStockCountGreaterThan(0);
        // goodsId 为传入商品的Id
        criteria.andGoodsIdEqualTo(goodsId);
        te.setOrderByClause(" is_default desc ");
        // 排序
        return tbItemMapper.selectByExample(te);
    }

    @Override
    public TbItem selectItemId(Long itemId) {
        return tbItemMapper.selectByPrimaryKey(itemId);
    }

    @Override
    public void updateStockCount(Long itemId, Integer num) {
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
        Integer count = tbItem.getStockCount();
        Integer xx = count-num;
        tbItem.setStockCount(xx);
        tbItemMapper.updateByPrimaryKeySelective(tbItem);
    }
}
